
import * as cc from "cc";
import { VMComponentEntity } from "./VMComponentEntity";

declare const CC_EDITOR;
declare const CC_DEV;

export class VMProgressEntity extends VMComponentEntity{
    
    // _updateValue(newValue,oldValue,watchPath){
        
    //     let _newValue = newValue;
    //     let _oldValue = oldValue;
    //     if(Array.isArray(watchPath)){
    //         _newValue = newValue[0]/newValue[1];
    //         if(oldValue === null || oldValue === undefined){
    //             _oldValue = 0;
    //         }else{
    //             _oldValue = oldValue[0]/oldValue[1];
    //         }
    //     }

    //     let val = this.formatValue(_newValue,_oldValue,this.node,0,watchPath);
        
    //     if(val>1)val = 1;
    //     if(val<0||Number.isNaN(val)){
    //         val = 0;
    //     }
    //     this.execComponent[this._property] = val;
    // }

    /**
     * 格式化值
     * @param newValue 
     * @param oldValue 
     * @param node 
     * @param nodeIdx 
     * @param watchPath 
     */
     formatValue(newValue: any,oldValue: any,node: cc.Node,nodeIdx: number,watchPath: string){
         let value = null;
        if(this.observerAttr.formator){
            value = this.observerAttr.formator.call(this.controllerUserComponent,{
             entity: this,
             newValue,
             oldValue,
             node,
             nodeIdx,
             watchPath
         });
        }else{
            
            let _newValue = newValue;
            let _oldValue = oldValue;
            if(Array.isArray(watchPath)){
                _newValue = newValue[0]/newValue[1];
                if(oldValue === null || oldValue === undefined){
                    _oldValue = 0;
                }else{
                    _oldValue = oldValue[0]/oldValue[1];
                }
            }
            value = _newValue;
            if(value>1)value = 1;
            if(value<0||Number.isNaN(value)){
                value = 0;
            }
        }
        return value;
    }
    
    get _property(){
        return 'progress';
    }
}